package com.zjdiepu.www.biz.fk;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.AuditEditStatus;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.BusinessExtrasCodeType;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.fk.FkAuditConditionEditMapper;
import com.zjdiepu.www.dao.fk.FkAuditConditionEditSubMapper;
import com.zjdiepu.www.dao.yw.YwBusinessExtrasMapper;
import com.zjdiepu.www.dao.yw.YwBusinessMapper;
import com.zjdiepu.www.ibiz.fk.IFkAuditConditionEditSubService;
import com.zjdiepu.www.model.fk.dto.FkAuditConditionEdit;
import com.zjdiepu.www.model.fk.dto.FkAuditConditionEditSub;
import com.zjdiepu.www.model.fk.ext.FkAuditConditionEditSubExt;
import com.zjdiepu.www.model.yw.dto.YwBusiness;

/**
 * fk_风控条件修改子表Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2018年01月31日 15时48分<br/>
 * @since 1.0<br/>
 */
@Service
public class FkAuditConditionEditSubServiceImpl extends BaseService implements IFkAuditConditionEditSubService {
	public static final Logger logger = LoggerFactory.getLogger(FkAuditConditionEditSubServiceImpl.class);

	@Autowired
	private FkAuditConditionEditSubMapper fkAuditConditionEditSubMapper;
	@Autowired
	private FkAuditConditionEditMapper fkAuditConditionEditMapper;
	@Autowired
	private YwBusinessMapper ywBusinessMapper;
	@Autowired
	private YwBusinessExtrasMapper ywBusinessExtrasMapper;
	
	@Override
	public void saveUpdateAndDelete(FkAuditConditionEditSub fkAuditConditionEditSub, OperateType operType) {
		switch (operType) {
		case add:
			fkAuditConditionEditSub.initInsertData(fkAuditConditionEditSub.getModifier(), fkAuditConditionEditSub.getModifierName());
			fkAuditConditionEditSubMapper.insert(fkAuditConditionEditSub);
			break;
		case edit:
			fkAuditConditionEditSub.initUpdateData(fkAuditConditionEditSub.getModifier(), fkAuditConditionEditSub.getModifierName());
			// 影响的记录数
			int updateCount = fkAuditConditionEditSubMapper.updateByPrimaryKeySelective(fkAuditConditionEditSub);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			fkAuditConditionEditSub.initDeleteData(fkAuditConditionEditSub.getModifier(), fkAuditConditionEditSub.getModifierName());
			// 影响的记录数
			int deleteCount = fkAuditConditionEditSubMapper.updateByPrimaryKeySelective(fkAuditConditionEditSub);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<FkAuditConditionEditSub> selectByExt(FkAuditConditionEditSubExt fkAuditConditionEditSubExt) {
		return fkAuditConditionEditSubMapper.selectByExt(fkAuditConditionEditSubExt);
	}
	
	@Override
	public int countByExt(FkAuditConditionEditSubExt fkAuditConditionEditSubExt){
		return fkAuditConditionEditSubMapper.countByExt(fkAuditConditionEditSubExt);
	}
	
	@Override
	public FkAuditConditionEditSub selectById(String id) {
		return fkAuditConditionEditSubMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			fkAuditConditionEditSubMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(FkAuditConditionEditSub fkAuditConditionEditSub) {
		return fkAuditConditionEditSubMapper.countRefined(fkAuditConditionEditSub);
	}

	@Override
	public FkAuditConditionEditSub selectRefined(FkAuditConditionEditSub fkAuditConditionEditSub) {
		return fkAuditConditionEditSubMapper.selectRefined(fkAuditConditionEditSub);
	}
	
	@Override
	public int deletedRefined(FkAuditConditionEditSub fkAuditConditionEditSub) {
		return fkAuditConditionEditSubMapper.deletedRefined(fkAuditConditionEditSub);
	}

	@Override
	public boolean repeat(FkAuditConditionEditSub fkAuditConditionEditSub) {
		int count = countRefined(fkAuditConditionEditSub);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(fkAuditConditionEditSub.getId())) {
			return true;
		}
		FkAuditConditionEditSub dbItem = selectRefined(fkAuditConditionEditSub);
		if(null == dbItem) {
			return false;
		}
		return !fkAuditConditionEditSub.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return fkAuditConditionEditSubMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<FkAuditConditionEditSub> fkAuditConditionEditSubs) {
		return fkAuditConditionEditSubMapper.insertBatch(fkAuditConditionEditSubs);
	}

	@Override
	public void saveFkAuditConditionEditSub(
			FkAuditConditionEditSub fkAuditConditionEditSub, AbstractSysUser currentUser) {
		
		String busiId=fkAuditConditionEditSub.getBusiId();
		String cusId=fkAuditConditionEditSub.getCusId();
		
		FkAuditConditionEdit fkAuditConditionEdit=new FkAuditConditionEdit();
		fkAuditConditionEdit.initInsertData(currentUser.getId(), currentUser.getName());
		YwBusiness ywBusiness=ywBusinessMapper.selectByPrimaryKey(busiId);
		if(ywBusiness!=null){
			fkAuditConditionEdit.setBusiStatus(ywBusiness.getBusiStatus());
			fkAuditConditionEdit.setAddedStatus1(ywBusiness.getAddedStatus1());
		}
		fkAuditConditionEdit.setBusiId(busiId);
		fkAuditConditionEdit.setCusId(cusId);
		fkAuditConditionEdit.setAuditEditStatus(AuditEditStatus.BC.getCode());
		fkAuditConditionEditMapper.insert(fkAuditConditionEdit);
		
		fkAuditConditionEditSub.setEditId(fkAuditConditionEdit.getId());
		
		List<FkAuditConditionEditSub> fkAuditConditionEditSubs=new ArrayList<FkAuditConditionEditSub>();
		FkAuditConditionEditSub fkAuditConditionEditSub1=new FkAuditConditionEditSub();
		fkAuditConditionEditSub1.setCodeExtras(BusinessExtrasCodeType.ADD_GPS.getCode());
		fkAuditConditionEditSub1.setValueExtrasNew(String.valueOf(fkAuditConditionEditSub.getAddGpsNew()));
		fkAuditConditionEditSub1.setValueExtrasOld(String.valueOf(fkAuditConditionEditSub.getAddGpsOld()));
		fkAuditConditionEditSub1.initInsertData(currentUser.getId(), currentUser.getName());
		fkAuditConditionEditSub1.setBusiId(busiId);
		fkAuditConditionEditSub1.setCusId(cusId);
		fkAuditConditionEditSub1.setEditId(fkAuditConditionEdit.getId());
		fkAuditConditionEditSub1.setRemarks(fkAuditConditionEditSub.getRemarks());
		fkAuditConditionEditSubs.add(fkAuditConditionEditSub1);
		
		FkAuditConditionEditSub fkAuditConditionEditSub2=new FkAuditConditionEditSub();
		fkAuditConditionEditSub2.setBusiId(busiId);
		fkAuditConditionEditSub2.setCusId(cusId);
		fkAuditConditionEditSub2.setEditId(fkAuditConditionEdit.getId());
		fkAuditConditionEditSub2.setCodeExtras(BusinessExtrasCodeType.ADD_GEPOSIT.getCode());
		fkAuditConditionEditSub2.setValueExtrasNew(String.valueOf(fkAuditConditionEditSub.getAddGepositNew()));
		fkAuditConditionEditSub2.setValueExtrasOld(String.valueOf(fkAuditConditionEditSub.getAddGepositOld()));
		fkAuditConditionEditSub2.initInsertData(currentUser.getId(), currentUser.getName());
		fkAuditConditionEditSub2.setRemarks(fkAuditConditionEditSub.getRemarks());
		fkAuditConditionEditSubs.add(fkAuditConditionEditSub2);
		
		FkAuditConditionEditSub fkAuditConditionEditSub3=new FkAuditConditionEditSub();
		fkAuditConditionEditSub3.setBusiId(busiId);
		fkAuditConditionEditSub3.setCusId(cusId);
		fkAuditConditionEditSub3.setEditId(fkAuditConditionEdit.getId());
		fkAuditConditionEditSub3.setCodeExtras(BusinessExtrasCodeType.IS_SPECIAL_ORDER.getCode());
		fkAuditConditionEditSub3.setValueExtrasNew(String.valueOf(fkAuditConditionEditSub.getIsSpecialOrderNew()));
		fkAuditConditionEditSub3.setValueExtrasOld(String.valueOf(fkAuditConditionEditSub.getIsSpecialOrderOld()));
		fkAuditConditionEditSub3.initInsertData(currentUser.getId(), currentUser.getName());
		fkAuditConditionEditSub3.setRemarks(fkAuditConditionEditSub.getRemarks());
		fkAuditConditionEditSubs.add(fkAuditConditionEditSub3);
		
		FkAuditConditionEditSub fkAuditConditionEditSub4=new FkAuditConditionEditSub();
		fkAuditConditionEditSub4.setBusiId(busiId);
		fkAuditConditionEditSub4.setCusId(cusId);
		fkAuditConditionEditSub4.setEditId(fkAuditConditionEdit.getId());
		fkAuditConditionEditSub4.setCodeExtras(BusinessExtrasCodeType.FLEXIBLECHARGE.getCode());
		fkAuditConditionEditSub4.setValueExtrasNew(String.valueOf(fkAuditConditionEditSub.getFlexibleChargeNew()));
		fkAuditConditionEditSub4.setValueExtrasOld(String.valueOf(fkAuditConditionEditSub.getFlexibleChargeOld()));
		fkAuditConditionEditSub4.initInsertData(currentUser.getId(), currentUser.getName());
		fkAuditConditionEditSub4.setRemarks(fkAuditConditionEditSub.getRemarks());
		fkAuditConditionEditSubs.add(fkAuditConditionEditSub4);
		fkAuditConditionEditSubMapper.insertBatch(fkAuditConditionEditSubs);
		
	}

	@Override
	public void updateFkAuditConditionEditSub(
			FkAuditConditionEditSub fkAuditConditionEditSub,
			AbstractSysUser currentUser) {
		
		FkAuditConditionEditSub fkAuditConditionEditSub1=new FkAuditConditionEditSub();
		fkAuditConditionEditSub1.setDpStatus(DpStatus.NORMAL.getCode());
		fkAuditConditionEditSub1.setEditId(fkAuditConditionEditSub.getEditId());
		fkAuditConditionEditSub1.setCodeExtras(BusinessExtrasCodeType.ADD_GPS.getCode());
		fkAuditConditionEditSub1=fkAuditConditionEditSubMapper.selectRefined(fkAuditConditionEditSub1);
		if(fkAuditConditionEditSub1!=null){
			fkAuditConditionEditSub1.setValueExtrasNew(String.valueOf(fkAuditConditionEditSub.getAddGpsNew()));
			fkAuditConditionEditSub1.setValueExtrasOld(String.valueOf(fkAuditConditionEditSub.getAddGpsOld()));
			fkAuditConditionEditSub1.initUpdateData(currentUser.getId(), currentUser.getName());
			fkAuditConditionEditSub1.setRemarks(fkAuditConditionEditSub.getRemarks());
			fkAuditConditionEditSub1.setCusId(fkAuditConditionEditSub.getCusId());
			fkAuditConditionEditSubMapper.updateByPrimaryKeySelective(fkAuditConditionEditSub1);
		}
		
		FkAuditConditionEditSub fkAuditConditionEditSub2=new FkAuditConditionEditSub();
		fkAuditConditionEditSub2.setDpStatus(DpStatus.NORMAL.getCode());
		fkAuditConditionEditSub2.setEditId(fkAuditConditionEditSub.getEditId());
		fkAuditConditionEditSub2.setCodeExtras(BusinessExtrasCodeType.ADD_GEPOSIT.getCode());
		fkAuditConditionEditSub2=fkAuditConditionEditSubMapper.selectRefined(fkAuditConditionEditSub2);
		if(fkAuditConditionEditSub2!=null){
			fkAuditConditionEditSub2.setValueExtrasNew(String.valueOf(fkAuditConditionEditSub.getAddGepositNew()));
			fkAuditConditionEditSub2.setValueExtrasOld(String.valueOf(fkAuditConditionEditSub.getAddGepositOld()));
			fkAuditConditionEditSub2.initUpdateData(currentUser.getId(), currentUser.getName());
			fkAuditConditionEditSub2.setRemarks(fkAuditConditionEditSub.getRemarks());
			fkAuditConditionEditSub2.setCusId(fkAuditConditionEditSub.getCusId());
			fkAuditConditionEditSubMapper.updateByPrimaryKeySelective(fkAuditConditionEditSub2);
		}
		
		FkAuditConditionEditSub fkAuditConditionEditSub3=new FkAuditConditionEditSub();
		fkAuditConditionEditSub3.setDpStatus(DpStatus.NORMAL.getCode());
		fkAuditConditionEditSub3.setEditId(fkAuditConditionEditSub.getEditId());
		fkAuditConditionEditSub3.setCodeExtras(BusinessExtrasCodeType.IS_SPECIAL_ORDER.getCode());
		fkAuditConditionEditSub3=fkAuditConditionEditSubMapper.selectRefined(fkAuditConditionEditSub3);
		if(fkAuditConditionEditSub3!=null){
			fkAuditConditionEditSub3.setValueExtrasNew(String.valueOf(fkAuditConditionEditSub.getIsSpecialOrderNew()));
			fkAuditConditionEditSub3.setValueExtrasOld(String.valueOf(fkAuditConditionEditSub.getIsSpecialOrderOld()));
			fkAuditConditionEditSub3.initUpdateData(currentUser.getId(), currentUser.getName());
			fkAuditConditionEditSub3.setRemarks(fkAuditConditionEditSub.getRemarks());
			fkAuditConditionEditSub3.setCusId(fkAuditConditionEditSub.getCusId());
			fkAuditConditionEditSubMapper.updateByPrimaryKeySelective(fkAuditConditionEditSub3);
		}
		
		FkAuditConditionEditSub fkAuditConditionEditSub4=new FkAuditConditionEditSub();
		fkAuditConditionEditSub4.setDpStatus(DpStatus.NORMAL.getCode());
		fkAuditConditionEditSub4.setEditId(fkAuditConditionEditSub.getEditId());
		fkAuditConditionEditSub4.setCodeExtras(BusinessExtrasCodeType.FLEXIBLECHARGE.getCode());
		fkAuditConditionEditSub4=fkAuditConditionEditSubMapper.selectRefined(fkAuditConditionEditSub4);
		if(fkAuditConditionEditSub4!=null){
			fkAuditConditionEditSub4.setValueExtrasNew(String.valueOf(fkAuditConditionEditSub.getFlexibleChargeNew()));
			fkAuditConditionEditSub4.setValueExtrasOld(String.valueOf(fkAuditConditionEditSub.getFlexibleChargeOld()));
			fkAuditConditionEditSub4.initUpdateData(currentUser.getId(), currentUser.getName());
			fkAuditConditionEditSub4.setRemarks(fkAuditConditionEditSub.getRemarks());
			fkAuditConditionEditSub4.setCusId(fkAuditConditionEditSub.getCusId());
			fkAuditConditionEditSubMapper.updateByPrimaryKeySelective(fkAuditConditionEditSub4);
		}
		
	}

	@Override
	public void commitFkAuditConditionEditSub(
			FkAuditConditionEditSub fkAuditConditionEditSub,
			AbstractSysUser currentUser) {
		FkAuditConditionEdit fkAuditConditionEdit=new FkAuditConditionEdit();
		fkAuditConditionEdit.setId(fkAuditConditionEditSub.getEditId());
		fkAuditConditionEdit.setAuditEditStatus(AuditEditStatus.DAISHENHE.getCode());
		fkAuditConditionEdit.initUpdateData(currentUser.getId(), currentUser.getName());
		fkAuditConditionEditMapper.updateByPrimaryKeySelective(fkAuditConditionEdit);
	}
}
